﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.TinhLuong;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.TinhLuongModel;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.ChamCongDelegate;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.TinhLuong;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.CommonDelegate;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers
{
    public class TinhLuongController : Controller
    {
        //
        // GET: /TinhLuong/

        #region private member
        TinhLuongHangThangServiceDelegate _tinhLuongHangThangServiceDelegate = new TinhLuongHangThangServiceDelegate();
        ChamCongServiceDelegate _chamCongServiceDelegate = new ChamCongServiceDelegate();
        CommonDelegate _CommonDelegate = new CommonDelegate();
        #endregion

        #region lập bảng tính lương

        [HttpGet]
        public ActionResult LapBangTinhLuong(int? id, string status)
        {
            LapBangTinhLuongForm form = new LapBangTinhLuongForm();
            if (id != null && id != -1 && id != 0)
                form = _tinhLuongHangThangServiceDelegate.BuildLapBangTinhLuong(id.Value);
            form.SelectListNam = _chamCongServiceDelegate.BuildSelectListNamDeTaoBangChamCong();
            form.SelectListThang = _chamCongServiceDelegate.BuildSelectListThangChuaCoBangChamCongTrongNamDeTaoBangChamCong(form.Nam);
            form.Id = id ?? -1;
            id = id ?? 0;
            if (!string.IsNullOrEmpty(status))
            {
                if (_CommonDelegate.EncodeSHA1("1" + id.Value) == status)
                    form.MessageSuccess = "Lập bảng chấn công thành công !";
                else if (_CommonDelegate.EncodeSHA1("2" + id.Value) == status)
                    form.MessageSuccess = "Tính lương chi tiết cho từng nhân viên thành công !";
            }
            return View(form);
        }
        [HttpPost]
        public ActionResult LapBangTinhLuong(LapBangTinhLuongForm form)
        {
            tb_DanhSachBangTinhLuong DanhSachTinhLuong = _tinhLuongHangThangServiceDelegate.BuildDanhSachTinhLuong(form);
            if (form.Id == -1)// tạo mới
            {
                DanhSachTinhLuong = _tinhLuongHangThangServiceDelegate.InsertDanhSachTinhLuong(DanhSachTinhLuong);
                if (DanhSachTinhLuong != null)
                    return Redirect("~/TinhLuong/LapBangTinhLuong/" + DanhSachTinhLuong.ID + "?status=" + _CommonDelegate.EncodeSHA1("1" + DanhSachTinhLuong.ID));
                else
                    form.MessageError = "Quá trình lưu gặp sự cố, lỗi trong quá trình lưu !";
            }
            else
            {
                DanhSachTinhLuong = _tinhLuongHangThangServiceDelegate.UpdateDanhSachTinhLuong(form);
                form.MessageSuccess = DanhSachTinhLuong != null ? "Cập nhật thành công!" : "Quá trình cập nhật gặp sự cố, lỗi trong quá trìn lưu";
            }
            form.SelectListNam = _chamCongServiceDelegate.BuildSelectListNamDeTaoBangChamCong();
            form.SelectListThang = _chamCongServiceDelegate.BuildSelectListThangChuaCoBangChamCongTrongNamDeTaoBangChamCong(form.Nam);
            return View(form);
        }

        [HttpPost]
        public ActionResult TaoBangTinhLuongChiTiet(LapBangTinhLuongForm form)//iddanhsachtinhluong
        {
            if (form.Id != 0)
            {
                tb_DanhSachBangTinhLuong DanhSachTinhLuong = _tinhLuongHangThangServiceDelegate.GetDanhSachTinhLuongById(form.Id);
                if (DanhSachTinhLuong.IsFinish || DanhSachTinhLuong.IsLock)
                    form.MessageError = "Không thể tạo vì bảng tính lương này đã tồn tại!";
                else
                    DanhSachTinhLuong = _tinhLuongHangThangServiceDelegate.LuuBangTinhLuongVaLapBangTinhLuongChiTiet(DanhSachTinhLuong);
                if (DanhSachTinhLuong != null)
                    return Redirect("~/TinhLuong/LapBangTinhLuong/" + DanhSachTinhLuong.ID + "?status=" + _CommonDelegate.EncodeSHA1("2" + DanhSachTinhLuong.ID));
                else
                    form.MessageError = "Quá trình lưu gặp sự cố, lỗi trong quá trình lưu !";
            }
            form.SelectListNam = _chamCongServiceDelegate.BuildSelectListNamDeTaoBangChamCong();
            form.SelectListThang = _chamCongServiceDelegate.BuildSelectListThangChuaCoBangChamCongTrongNamDeTaoBangChamCong(form.Nam);
            return View("~/Views/TinhLuong/LapBangTinhLuong.aspx",form);

        }

        public JsonResult CheckExistThangNamAjax(string thang, string nam)
        {
            bool kq = _tinhLuongHangThangServiceDelegate.CheckExistThangNam(thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CheckExistLuongChiTietThangNamAjax(int id, string thang, string nam)
        {
            bool kq = _tinhLuongHangThangServiceDelegate.CheckExistLuongChiTietThangNam(id,thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Tổng hợp lại dữ liệu

        public JsonResult TongHopLaiDuLieuCuaCacBan(int thang, int nam)//tổng hợp lại dữ liệu các bảng update vào bảng tính lương chi tiết
        {
            bool kq = _tinhLuongHangThangServiceDelegate.UpdateAllTinhLuongChitietTrongThang(thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region danh sách tháng tính lương
        public ActionResult DanhSachBangTinhLuong(int? pagednumber)
        {
            LapBangTinhLuongForm form = new LapBangTinhLuongForm();
            form.DanhSachTinhLuongPagedList = _tinhLuongHangThangServiceDelegate.GetDanhSachTinhLuongPagedList(pagednumber);
            return View(form);
        }

        #endregion

        #region tính lương chi tiết của từng tháng
        public ActionResult TinhLuongHangThang(int? id, int? thang, int? nam, string d)
        {
            thang = thang ?? DateTime.Today.Month;
            nam = nam ?? DateTime.Today.Year;
            TinhLuongHangThangForm form = new TinhLuongHangThangForm();
            form = _tinhLuongHangThangServiceDelegate.BuildTinhLuongHangThang(id, thang.Value, nam.Value, !string.IsNullOrEmpty(d) ? true : false);
            return View(form);
        }
        public JsonResult SuaTinhLuongTungNhanVienAjax(int id, string myArray)
        {
            string[] str = _tinhLuongHangThangServiceDelegate.SuaTinhLuongTungNhanVien(id, myArray);
            return Json(str, JsonRequestBehavior.AllowGet);
        }

        public JsonResult XoaThangLuongDuocTinh(int id, int thang, int nam)//id là IdBangTinhLuong
        {
            bool kq = _tinhLuongHangThangServiceDelegate.XoaThangTinhLuong(id, thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        public JsonResult KhoaThangLuongDuocTinh(int id, int thang, int nam)//id là IdBangTinhLuong
        {
            bool kq = _tinhLuongHangThangServiceDelegate.KhoaThangTinhLuong(id, thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        public JsonResult MoKhoaThangLuongDuocTinh(int id, int thang, int nam)//id là IdBangTinhLuong
        {
            bool kq = _tinhLuongHangThangServiceDelegate.MoKhoaThangTinhLuong(id, thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        public JsonResult HoanThanhThangLuongDuocTinh(int id, int thang, int nam)//id là IdBangTinhLuong
        {
            bool kq = _tinhLuongHangThangServiceDelegate.HoanThanhThangTinhLuong(id, thang, nam);
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}
